#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
#define LL long long
#define XH(i,a,b) for(int i = a; i < b; ++i)
#define XXH(i,b,a) for(int i = b; i >= a; --i)
int tmpInt;LL tmpLL;string tmpStr;char tmpChar;
vector<int>tmpIntVector;
struct node{
  
};

int main(){
  int t;cin>>t;
  for (int i= 0; i<t; ++i) {
    int n; cin>>n;
    if(n%2 == 0){
      XH(j,0,n*(n-1)/2){printf("0%c",j == n*(n-1)/2-1 ? '\n':' ');}
    }else{
      for (int j= n - 1;j>0; --j) {
       for (int k = 0; k < j; ++k) {
           if(k%2)printf("-1");
           else printf("1");
           if(j == 1 && k == j-1)printf("\n");
           else printf(" ");
       } 
      }
    }
  }

  return 0;
}
